﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows;

namespace ShapeDemo
{
    class ASPNETX_Adapter
    {
        public static string SerializePolygon(List<ASPNETX_WPFPolygon> lstPolygon)
        {
            string result="";

            List<ASPNETX_Polygon> sd_polygon = new List<ASPNETX_Polygon>();

            foreach (ASPNETX_WPFPolygon item in lstPolygon)
            {
                ASPNETX_Polygon _polygon = new ASPNETX_Polygon();
                _polygon.CommonProperty = item.CommonProperty;
                _polygon.X = 0;
                _polygon.Y = 0;

                foreach (Point pitem in item.PolygonBody.Points)
                {
                    DPoint _p = new DPoint();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polygon.PointsList.Add(_p);
                }

                sd_polygon.Add(_polygon);
            }

            result=SimpleSerializer.Serialize<List<ASPNETX_Polygon>>(sd_polygon);

            return result;
        }

        public static List<ASPNETX_WPFPolygon> DeSerializePolygon(string strxml)
        {
            List<ASPNETX_WPFPolygon> result = new List<ASPNETX_WPFPolygon>();

            List<ASPNETX_Polygon> sd_polygon = new List<ASPNETX_Polygon>();
            sd_polygon = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Polygon>),strxml) as List<ASPNETX_Polygon>;

            foreach (ASPNETX_Polygon item in sd_polygon)
            {
                ASPNETX_WPFPolygon _polygon = new ASPNETX_WPFPolygon();
                _polygon=ResourceCenter.NewPolygon();
                _polygon.PolygonBody.Points.Clear();
                _polygon.PolygonBody.Name = "P_" + item.CommonProperty.Index.ToString();
                _polygon.CommonProperty = item.CommonProperty;
                _polygon.PolygonBody.Opacity = 0.1;
                foreach (DPoint pitem in item.PointsList)
                {
                    Point _p = new Point();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polygon.PolygonBody.Points.Add(_p);
                }

                result.Add(_polygon);
            }

            return result;
        }



    }
}
